package be.sbw.wave.extensions.publisher.model;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Text;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class WaveDocument {

	private static final String WAVE_DOCUMENT_PREFIX = "WAVE_DOC_";

	@PrimaryKey
	@Persistent
	private String id;
	
	@Persistent
	private String waveId;
	
	@Persistent
	private String initiator;
	
	@Persistent
	private String authors;
	
	@Persistent
	private String waveletId;
	
	@Persistent
	private String blipId;
	
	@Persistent
	private String blipTitle;
	
	@Persistent
	private Text blipContent;
	
	@Persistent
	private Text blipHtmlContent;
	
	public WaveDocument(String waveId, String waveletId, String blipId, String initiator, String authors, String blipTitle, String blipContent, String blipHtmlContent){
		this.setId(WaveDocument.getMD5(waveId));
		this.setWaveId(waveId);
		this.setWaveletId(waveletId);
		this.setBlipId(blipId);
		this.setInitiator(initiator);
		this.setAuthors(authors);
		this.setBlipTitle(blipTitle);
		this.setBlipContent(blipContent);
		this.setBlipHtmlContent(blipHtmlContent);
	}
	
	public static WaveDocument GetWaveByWaveId(PersistenceManager manager,String id){
		String md5= WaveDocument.getMD5(id);
	    Query query = manager.newQuery(WaveDocument.class);
	    query.setFilter("id == idParam");
	    query.declareParameters("String idParam");
	    String idParam = WaveDocument.WAVE_DOCUMENT_PREFIX + md5;
	    List<WaveDocument> waves = (List<WaveDocument>) query.execute(idParam);
	    if (!waves.isEmpty() && waves.get(0) != null) {
	      return waves.get(0);
	    }
	    return null;
	}
	
	public static WaveDocument GetWave(PersistenceManager manager,String md5){
	    Query query = manager.newQuery(WaveDocument.class);
	    query.setFilter("id == idParam");
	    query.declareParameters("String idParam");
	    String idParam = WaveDocument.WAVE_DOCUMENT_PREFIX + md5;
	    List<WaveDocument> waves = (List<WaveDocument>) query.execute(idParam);
	    if (!waves.isEmpty() && waves.get(0) != null) {
	      return waves.get(0);
	    }
	    return null;
	}
	
	private static String getMD5(String waveId){
		String result="";
		MessageDigest m;
		try {
			m = MessageDigest.getInstance("MD5");
			m.update(waveId.getBytes(),0,waveId.length());
			result = new BigInteger(1,m.digest()).toString(16);
		} catch (NoSuchAlgorithmException e) {

		}
		return result;
	}
	
	public void setWaveId(String waveId){
		this.waveId = waveId;
	}
	
	public String getWaveId(){
		return this.waveId;
	}

	public void setInitiator(String author) {
		this.initiator = author;
	}

	public String getInitiator() {
		return initiator;
	}

	public void setWaveletId(String waveletId) {
		this.waveletId = waveletId;
	}

	public String getWaveletId() {
		return waveletId;
	}

	public void setBlipTitle(String blipTitle) {
		this.blipTitle = blipTitle;
	}

	public String getBlipTitle() {
		return blipTitle;
	}

	public void setBlipContent(String blipContent) {
		this.blipContent = new Text(blipContent);
	}

	public String getBlipContent() {
		return blipContent.getValue();
	}

	public void setBlipId(String blipId) {
		this.blipId = blipId;
	}

	public String getBlipId() {
		return blipId;
	}

	public void setAuthors(String authors) {
		this.authors = authors;
	}

	public String getAuthors() {
		return authors;
	}

	private void setId(String id) {
		this.id = WAVE_DOCUMENT_PREFIX+id;
	}

	public String getId() {
		return id.replace(WAVE_DOCUMENT_PREFIX, "");
	}

	public void setBlipHtmlContent(String blipHtmlContent) {
		this.blipHtmlContent = new Text(blipHtmlContent);
	}

	public String getBlipHtmlContent() {
		return blipHtmlContent.getValue();
	}
	
}
